package sample.test.cc.cool;

import java.util.HashSet;
import java.util.Set;

import sample.test.cc.cool.gui.controllers.ClientController;
import sample.test.cc.simpleagent.ReadGuard;
import sample.test.cc.simpleagent.SimpleDataRead;
import sample.test.cc.simpleagent.SimpleDataWrite;
import sample.test.cc.simpleagent.WriteGuard;
import BESA.Exception.ExceptionBESA;
import BESA.SystemLevel.AdmBESA;
import BESA.SystemLevel.Directory.AgHandler;
import BESA.SystemLevel.transactions.agent.Intention;
import BESA.SystemLevel.transactions.agent.TransactionalEventBESA;

public class TransactionManager {

	private AdmBESA admin;

	public TransactionManager() {
		try {
			setAdmin(AdmBESA.getInstance());
		} catch (UnsupportedOperationException e) {
			ClientController.getInstance().showCreateDialog();
		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	public void execTransaction(long idTransaction, String alias, Intention in,
			Integer... value) {
		System.err.println("___MODEL");
		try {
			System.out.println("T_ID :::::::::::" + idTransaction);
			AgHandler ah = admin.getHandlerByAlias(alias);
			TransactionalEventBESA ev = null;
			if (in.toString().equals("READ")) {
				String evType = ReadGuard.class.getName();
				SimpleDataRead sdr = new SimpleDataRead();
				ev = new TransactionalEventBESA(evType,
						sdr, idTransaction);
			} else if (in.toString().equals("WRITE")) {
				String evType = WriteGuard.class.getName();
				SimpleDataWrite sdr = new SimpleDataWrite(value[0]);
				ev = new TransactionalEventBESA(evType,
						sdr, idTransaction);
			}
			if(ev!= null){
				new Thread(new innerSender(ah, ev)).start();
			}
				
			System.err.println("______ END MODEL");
			Thread.sleep(900);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public long openTransaction() {
		try {
			return getAdmin().openTransaction();
		} catch (ExceptionBESA e) {
			e.printStackTrace();
		}
		return Long.MAX_VALUE;
	}

	public boolean closeTransaction(long id) {
		return admin.closeTransaction(id);
	}

	@SuppressWarnings("unchecked")
	public Set<String> getAllAgentsAlias() {
		if(getAdmin().getAgAliasTable()!= null && getAdmin().getAgAliasTable().keySet()!= null){
			return (Set<String>) getAdmin().getAgAliasTable().keySet();
		}
		return new HashSet<String>();
	}

	public void setAdmin(AdmBESA instance) {
		this.admin = instance;
	}

	public AdmBESA getAdmin() {
		if(admin==null){
			admin = AdmBESA.getInstance();
		}
		return admin;
	}
	
	class innerSender implements Runnable{

		AgHandler agh ;
		TransactionalEventBESA ev;
		
		
		
		public innerSender(AgHandler agh, TransactionalEventBESA ev) {
			super();
			this.agh = agh;
			this.ev = ev;
		}



		@Override
		public void run() {
			
			try {
				agh.sendEvent(ev);
			} catch (ExceptionBESA e) {
				e.printStackTrace();
			}
		}
		
	}
}
